package com.brush.common.core.controller;


import com.brush.common.core.domain.R;
import com.brush.common.utils.DateUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import java.beans.PropertyEditorSupport;
import java.util.Date;

/**
 * 封装通用控制器
 */
public class BaseController {

    /**
     * 将前台传递过来的日期格式的字符串，自动转化为Date类型
     */
    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        // Date 类型转换
        binder.registerCustomEditor(Date.class, new PropertyEditorSupport()
        {
            @Override
            public void setAsText(String text)
            {
                setValue(DateUtils.parseDate(text));
            }
        });
    }


    public R isSuccess(boolean flag){
       return flag? R.ok() : R.fail();
    }

    public R isSuccess(Integer flag){
        return flag>0? R.ok() : R.fail();
    }

}
